package com.jiawa.wiki.controller;

import com.jiawa.wiki.req.DocQueryReq;
import com.jiawa.wiki.req.DocSaveReq;
import com.jiawa.wiki.resp.DocQueryResp;
import com.jiawa.wiki.resp.CommonResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.service.DocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/doc")
public class DocController {

    @Autowired
    private DocService docService;


    @GetMapping("/list")
    public CommonResp<PageResp<DocQueryResp>> list (@Valid DocQueryReq req){
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
        resp.setContent(docService.list(req));
        return resp;
    }

    @GetMapping("/all/{ebookId}")
    public CommonResp<List<DocQueryResp>> all (@PathVariable Long ebookId){
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
        resp.setContent(docService.all(ebookId));
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save (@Valid @RequestBody DocSaveReq req){
        CommonResp resp = new CommonResp<>();
        docService.save(req);
        return resp;
    }

    @DeleteMapping ("/delete/{idsStr}")
    public CommonResp detele (@PathVariable String idsStr){
        CommonResp resp = new CommonResp<>();
        List<String> list = Arrays.asList(idsStr.split(",")) ;
        docService.delete(list);
        return resp;
    }

    @GetMapping("/find-content/{id}")
    public CommonResp findContent (@PathVariable Long id){
        CommonResp<String> resp = new CommonResp<>();
        resp.setContent(docService.findContent(id));
        return resp;
    }

    /**
     * 点赞
     * @param id
     * @return
     */
    @GetMapping("/vote/{id}")
    public CommonResp vote (@PathVariable Long id){
        CommonResp<String> resp = new CommonResp<>();
        docService.vote(id);
        return resp;
    }
}
